﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FrmHotel
{
    public partial class FrmSuiKe : Form
    {
        public FrmSuiKe()
        {
            InitializeComponent();
        }
        public int id;//接收房间号
        private void FrmSuiKe_Load(object sender, EventArgs e)
        {
            BoinComBox();
            BoinDataGridView();
            SataInfo();
        }

        public void SataInfo()
        {
            string sql = string.Format("select r.RoomName from Room r,RoomInfo o where o.RmRoom=r.RoomId and o.RmId={0}", id);
            string r = DBHelper.SelectOne(sql).ToString();
            this.label11.Text = r;
            string sql1 = string.Format("select RmId from  RoomInfo  where RmId={0}", id);
            string r1 = DBHelper.SelectOne(sql1).ToString();
            this.label9.Text = r1;
        }
        public void BoinComBox()
        {
            string sql = "select * from  CetaeClass";
            DataTable dt = DBHelper.SelectForTable(sql);
            DataRow dr = dt.NewRow();
            dr["CetaeId"] = 0;
            dr["CetaeName"] = "请选择";
            dt.Rows.InsertAt(dr, 0);
            this.comboBox1.DisplayMember = "CetaeName";
            this.comboBox1.ValueMember = "CetaeId";
            this.comboBox1.DataSource = dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = this.txt_name.Text;//主客名称
            string gender = this.radioButton1.Checked ? "男" : "女";//性别
            int age = Convert.ToInt32(this.numericUpDown1.Value);//年龄
            int zhengjian = Convert.ToInt32(this.comboBox1.SelectedValue);//证件类型
            string bianhaoH = this.txt_zhengjainhao.Text;//证件编号
            string Address = this.txt_Address.Text;//住址
            string hoper = this.txt_hoper.Text;//联系电话

            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(hoper) || string.IsNullOrEmpty(bianhaoH) || string.IsNullOrEmpty(Address))
            {
                MessageBox.Show("请填写完整的入住信息！", "提示信息！");
                return;
            }
            string sql5 = string.Format("select count(1) from SuiKeInfo  where SkHao='{0}' and SkAddress='{1}' and SkPhone='{2}' ", bianhaoH, Address, hoper);
            int re1 = Convert.ToInt32(DBHelper.SelectOne(sql5));
            if (re1 >= 1)
            {
                MessageBox.Show("此随客已入住！", "提示信息！");
                return;
            }
            string sql = string.Format(@"insert into SuiKeInfo values({0},'{1}','{2}',{3},'{4}','{5}',{6},'{7}')",
                                   id, name, gender, age, hoper, Address,zhengjian, bianhaoH);
            int r = Convert.ToInt32(DBHelper.ExecuteUpdate(sql));
            if (r>0)
            {
                  MessageBox.Show("入住成功！","提示信息！");
                    BoinDataGridView();
            }
            else
            {
                MessageBox.Show("入住失败！", "提示信息！");
            }
        }
        public void BoinDataGridView()
        {
            this.dataGridView1.AutoGenerateColumns = false;
            string sql = string.Format("select r.RmId,s.SkName,s.SkPhone,s.SktaeClass,c.CetaeName,s.SkHao from  SuiKeInfo s,RoomInfo r,CetaeClass c where c.CetaeId=s.SktaeClass and  s.SkFangHao=r.RmId and r.RmId={0}  and 1=1",id);
            string hao = this.textBox2.Text;
            if (!string.IsNullOrEmpty(hao))
            {
                sql += string.Format(" and s.SkPhone like '%{0}%' ", hao);
            }
            string name = this.textBox1.Text;
            if (!string.IsNullOrEmpty(name))
            {
                sql += string.Format(" and s.SkName like '%{0}%' ", name);
            }
            this.dataGridView1.DataSource = DBHelper.SelectForTable(sql);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            BoinDataGridView();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            BoinDataGridView();
        }
    }
}
